﻿using DDD.Application;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using AutoMapper;
using GoodsModule.Domain.GoodsAggregate;
using DDD.Domain;

namespace GoodsModule.Application.Commands
{
    [AutoMapperTo(typeof(Domain.GoodsAggregate.Goods))]
    public class ChangeGoodsCommand : ICommand<Domain.GoodsAggregate.Goods>
    {
        public int Id { get; set; }

        public byte[] RowVersion { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Range(1, 999)]
        public int GoodsCategoryId { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        [DisplayName("标题")]
        [Comment("标题")]
        [Required]
        public string Title { get; set; } = string.Empty;
        /// <summary>
        /// 状态
        /// </summary>
        [Comment("状态")]
        [Required]
        [EnumDataType(typeof(Domain.GoodsStatus))]
        public Domain.GoodsStatus Status { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        [Comment("排序")]
        [Required]
        public uint Sort { get; set; } = 1000;

        /// <summary>
        /// 开始时间
        /// </summary>
        [Comment("开始时间")]
        [Required]
        [DataType(DataType.DateTime)]
        public DateTime StartTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 结束时间
        /// </summary>
        [Comment("结束时间")]
        [Required]
        [DataType(DataType.DateTime)]
        public DateTime EndTime { get; set; } = DateTime.MaxValue;

        /// <summary>
        /// 经验
        /// </summary>
        [Comment("经验")]
        [Required]
        [Precision(9, 2)]
        public decimal Exp { get; set; } = 0;

        /// <summary>
        /// 积分
        /// </summary>
        [Comment("积分")]
        [Required]
        [Precision(9, 2)]
        public decimal Point { get; set; } = 0;

        /// <summary>
        /// 金额
        /// </summary>
        [Comment("金额")]
        [Required]
        [Precision(9, 2)]
        public decimal Price { get; set; } = 0;

        /// <summary>
        /// 内容
        /// </summary>
        [Comment("内容")]
        [Required]
        public string Content { get; set; } = string.Empty;
    }

    public class ChangeGoodsCommandHandler(Infrastructure.Repositories.GoodsRepository repository, IMapper mapper, ILogger<ChangeGoodsCommandHandler> logger)
        : ICommandHandler<ChangeGoodsCommand, Domain.GoodsAggregate.Goods>
    {
        public async Task<Goods> Handle(ChangeGoodsCommand request, CancellationToken cancellationToken)
        {
            var goods = await repository.GetAll().FirstOrDefaultAsync(e => e.Id == request.Id);
            if (goods == null)
            {
                throw new InvalidOperationException($"找不到id为{request.Id}的商品");
            }
            //goods.ChangeSort(request.Sort);
            //TODO: 这里有问题 不会触发其他领域事件
            goods.ChangeEntity(mapper.Map<Goods>(request));
            //TODO: RowVersion没起作用？
            goods = await repository.UpdateAsync(goods);
            logger.LogInformation("ChangeGoodsCommandHandler Goods {}", goods.Id);
            return goods;
        }
    }
}
